package com.lockapp.database;

import com.lockapp.entity.Block;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class BlockDAO extends LockAppDatabase {

	public BlockDAO(Context context) {
		super(context);
	}

	public BlockCursor getAll(){
		SQLiteDatabase dataBase = getReadableDatabase();
		BlockCursor cursor = (BlockCursor) dataBase.
				rawQueryWithFactory(new BlockCursor.Factory(), BlockCursor.QUERY, null, null);
		cursor.moveToFirst();
		for (int i = 0; i < cursor.getCount(); i++) {
			cursor.moveToPosition(i);
			Log.i("AQUIIIIIIIII", ""+cursor.getLiberacao());
		}
		return cursor;
	}

	public BlockCursor getBlockId(int id){
		SQLiteDatabase dataBase = getReadableDatabase();
		BlockCursor cursor = (BlockCursor) dataBase.
				rawQueryWithFactory(new BlockCursor.Factory(), "select * from block where idblock = "+id, null, null);
		cursor.moveToFirst();
		return cursor;
	}
	
	public void inserir(Block block){
		String sql = "insert into block (nomeapp, pacoteapp, iconeapp, liberacao) values (?,?,?,?)";
		Object[] obj = new Object[]{block.getNomeApp(), block.getPacoteApp(), block.getArrayImg(), block.getLiberacao()};
		try {
			getWritableDatabase().execSQL(sql, obj);
		} catch (Exception e) {
			Log.e("ERRO AO INSERIR UM APP BLOQUEADO", e.getMessage());
		}
	}

	public void excluir(int id){
		String sql = "delete from block where idblock = ?";
		Object[] obj = new Object[]{id};
		try {
			getWritableDatabase().execSQL(sql, obj);
		} catch (Exception e) {
			Log.e("ERRO AO EXCLUIR UM APP BLOQUEADO", e.getMessage());
		}
	}
	
	public void alterar(Block block){
		String sql = "update block set liberacao = ? where idblock = ?";
		Object[] obj = new Object[]{block.getLiberacao(), block.getIdBlock()};
		try {
			getWritableDatabase().execSQL(sql, obj);
		} catch (Exception e) {
			Log.e("ERRO AO ALTERAR UM APP BLOQUEADO", e.getMessage());
		}
	}
	
}
